package com.taocyb.core.uc.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.taocyb.commons.uc.entity.User;
import com.taocyb.commons.uc.entity.UserDetail;
import com.taocyb.core.uc.dao.UserDao;
import com.taocyb.core.uc.dao.UserDetailDao;

@Component
@Transactional(readOnly = true)
public class UserManager{

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserDetailDao userDetailDao;
	
	/**
	 * 根据用户ID查询用户
	 */
	public User findUserById(Long id) {
		return userDao.findById(id);
	}
	
	/**
	 * 根据用户账号查询用户
	 */
	public User findUserByLoginName(String loginName){
		return userDao.findByLoginName(loginName);
	}
	
	/**
	 * 新增用户
	 */
	@Transactional(readOnly = false)
	public Long saveUser(User user, UserDetail userDetail){
		userDao.save(user);
		Long userId = user.getId();

		userDetail.setUserId(userId);
		userDetailDao.save(userDetail);
		return userId;
	}
	
	/**
	 * 更新用户 
	 */
	@Transactional(readOnly = false)
	public void updateUser(User user){
		userDao.update(user);
	}
	
	/**
	 * 根据用户账号查询用户详情
	 */
	public UserDetail findUserDetailByLoginName(String loginName){
		return userDetailDao.findByLoginName(loginName);
	}
}